package net.bplaced.streetfighter.adapter;

import java.util.List;

import net.bplaced.streetfighter.R;
import net.bplaced.streetfighter.lib.model.MainMenuEntry;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author Sebastian Hoffmann <bo.hoffi@googlemail.de>
 */
public class MainMenuEntryAdapter
        extends BaseAdapter {

    private final Context context;
    private final List<MainMenuEntry> menuEntries;

    public MainMenuEntryAdapter(final Context context, final List<MainMenuEntry> menuEntries) {

        this.context = context;
        this.menuEntries = menuEntries;
    }

    @Override
    public int getCount() {

        return this.menuEntries.size();
    }

    @Override
    public MainMenuEntry getItem(int position) {

        return this.menuEntries.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final MainMenuEntry e = this.menuEntries.get(position);

        if (convertView == null) {
            final LayoutInflater inflater =
                    (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.main_menu_adapter_item, null);
        }

        final ImageView iView = (ImageView) convertView.findViewById(R.id.main_menu_adapter_item_image);
        iView.setImageResource(e.getDrawableId());

        final TextView infoField = (TextView) convertView.findViewById(R.id.main_menu_adapter_item_info);
        infoField.setText(e.getEntryCaption());

        if (e.getIsActive()) {
            convertView.setTag(e);
        }

        return convertView;
    }
}